using AuthenticationTest;
using AutoMapper;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using System.Text;
using WidgetsDesign.API.DataBase;
using WidgetsDesign.API.Helper;
using WidgetsDesign.API.Models;
using WidgetsDesign.API.Profiles;
using WidgetsDesign.API.Services;


var builder = WebApplication.CreateBuilder(args);
var configuration = builder.Configuration;
builder.Services.AddAuthentication(option =>
{
    option.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(option =>
{
    option.TokenValidationParameters = new TokenValidationParameters()
    {
        ValidateIssuer = true, //??????Issuer
        ValidIssuer = configuration["Jwt:Issuer"], //??????Issuer
        ValidateAudience = true, //??????Audience
        ValidAudience = configuration["Jwt:Audience"], //??????Audience
        ValidateIssuerSigningKey = true, //??????SecurityKey
        IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(configuration["Jwt:SecretKey"])), //SecurityKey
        ValidateLifetime = true, //???????????
        ClockSkew = TimeSpan.FromSeconds(30), //????????????????????????????????????
        RequireExpirationTime = true,
    };
});
// Add services to the container.
builder.Services.AddAutoMapper(typeof(HitokotoProfile).Assembly);
builder.Services.AddControllers();
builder.Services.AddTransient<IHitokotoRepository,HitokotoRepository>();
builder.Services.AddTransient<IUserRepository,UserRepository>();
builder.Services.AddTransient<IUploadFileRepository,UploadFileRepository>();
builder.Services.AddTransient<IWidgetsListRepository,WidgetsListRepository>();
builder.Services.AddTransient<IRechargeRepository,RechargeRepository>();
builder.Services.AddScoped<IDictRepository, DictRepository>();
builder.Services.AddSingleton(new JwtHelper(configuration));
var config = new MapperConfiguration(cfg =>
{
    cfg.AddProfile<HitokotoProfile>();
});

var connectionString = $" server=112.124.55.97;port=3306;database=WidgetsDesign;user id=root;password=Walker7355608; SslMode=None ";
//var ConnectionString = $" server=172.17.0.2;port=3306;database=WidgetsDesign;user id=root;password=Walker7355608; SslMode=None ";

builder.Services.AddDbContext<AppDbContext>(option =>
{
    option.UseMySql(connectionString, new MySqlServerVersion(new Version(8,0,27)));
});
var MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
builder.Services.AddCors(options =>
{
    options.AddPolicy(name: MyAllowSpecificOrigins,
                      policy =>
                      {
                          policy.AllowAnyOrigin();
                          policy.AllowAnyHeader();
                          policy.AllowAnyMethod();
                      });
});
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
app.UseStaticFiles();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}
app.UseAuthentication();
app.UseAuthorization();

app.UseHttpsRedirection();

app.UseRouting();

app.UseCors(MyAllowSpecificOrigins);

app.UseAuthorization();

app.MapControllers();

app.Run();
